/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation.experiment;

import ca.uwindsor.cs.deepweb.estimation.DictionariesFilesContentIndexer;
import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

/**
 * Manage the entire Index job
 * 
 * @author Liang Jie
 */
public class IndexRunner {

	protected FilesContentIndexer indexer;

	protected String sourcefiles;

	protected String indexDir;

	protected String encoding;

	protected boolean isRecursive;

	public IndexRunner(String sourcedir, String indexdir) {
		this(sourcedir, indexdir, "UTF-8", false, true, new String[] { "txt" });
	}

	public IndexRunner(String sourcedir, String indexdir, String encode,
			boolean isVerbose, boolean isCreate, String[] ext) {
		sourcefiles = sourcedir;
		indexDir = indexdir;
		encoding = encode;
		indexer = new FilesContentIndexer(sourcedir, indexdir, encode,
				isVerbose, isCreate, ext);
	}

	public IndexRunner(String sourcedir, String indexdir, String encode,
			boolean isVerbose, boolean isCreate, String[] ext, boolean recursive) {
		sourcefiles = sourcedir;
		indexDir = indexdir;
		encoding = encode;
		if (recursive) {
			indexer = new DictionariesFilesContentIndexer(sourcedir, indexdir,
					encode, isVerbose, isCreate, ext);
		}
	}

	public void runIndexer() {
		System.out.println("Start to Index!");
		System.out.println("Indexing from " + sourcefiles + " to " + indexDir
				+ "...");
		indexer.generateIndex();
		System.out.println("Indexing is completed!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		args = new String[7];
//		args[0] = "--SourceDir=D:\\Research\\estimation\\Corpus\\ChineseNovels\\ToBeIndexed";
//		args[1] = "--IndexDir=D:\\Research\\estimation\\indexes\\zh_novels";
//		args[2] = "--encoding=GB2312";
//		args[3] = "-verbose";
//		args[4]= "-create";
//		args[5]= "-resursive";
//		args[6]= "--extension=txt,htm,html,xml";

		String SourceDir = new String();
		String IndexDir = new String();
		String encoding = new String("UTF-8");
		boolean isVerbose = false;
		boolean isCreate = true;
		boolean isRecursive = false;

		String ext[] = new String[] { "txt" };
		if (args.length < 2) {
			System.out
					.println("usage: java -jar IndexRunner --SourceDir=sourcefiledirectory --IndexDir=indexfiledirectory --extension=txt,html,... [--encoding=encoding](default=UTF-8) [-verbose] [-create] [-recursive]!");
			System.exit(0);
		} else {
			for (int i = 0; i < args.length; i++) {
				if (args[i].startsWith("--SourceDir=")) {
					SourceDir = args[i].substring(12);
				} else if (args[i].startsWith("--IndexDir=")) {
					IndexDir = args[i].substring(11);
				} else if (args[i].startsWith("--encoding=")) {
					encoding = args[i].substring(11);
				} else if (args[i].equals("-create")) {
					isVerbose = true;
				} else if (args[i].equals("-verbose")) {
					isCreate = true;
				} else if (args[i].startsWith("--extension=")) {
					String tempexts = args[i].substring(12);
					ext = tempexts.split(",");
				} else if (args[i].endsWith("-resursive")) {
					isRecursive = true;
				}
			}
		}

		if (SourceDir.equals("") || IndexDir.equals("")) {
			System.out
					.println("usage: java -jar IndexRunner SourceDir=sourcefiledirectory IndexDir=indexfiledirectory [encoding=encoding](default=UTF-8) [-verbose]!");
			System.exit(0);
		} else {
			IndexRunner indexer;
			if (isRecursive) {
				indexer = new IndexRunner(SourceDir, IndexDir, encoding,
						isVerbose, isCreate, ext, isRecursive);
			} else {
				indexer = new IndexRunner(SourceDir, IndexDir, encoding,
						isVerbose, isCreate, ext);
			}
			indexer.runIndexer();
		}

	}

}
